#include "uart.h"

//~~~~~~~~~~~~~~~~~~~~~~~~~~USART1~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
static struct rcc_apb_periph uart1_apb[]=
{
    {
        2,RCC_APB2ENR_USART1EN_Pos
    },
    {
        2,RCC_APB2ENR_IOPAEN_Pos,
    },
    {
        2,RCC_APB2ENR_AFIOEN_Pos,
    },
    {
        0,0,
    }
};

static struct uart_gpio uart1_gpio=
{
    {
        GPIOA_BASE,9,GPIO_MODE_AF_PP,GPIO_SPEED_FREQ_HIGH,GPIO_PULLUP,
    },
    {
        GPIOA_BASE,10,GPIO_MODE_AF_INPUT,GPIO_SPEED_FREQ_HIGH,GPIO_PULLUP,
    },
};


static sj_char uart1_rx_buffer[UART_BUFFER_CACHE_SIZE]={0};
static struct uart_device_data uart1_data={
    &uart1_apb[0],
    &uart1_gpio,
    {
        USART1_IRQn,2,1
    },
    {
        USART1_BASE,
        115200,
        UART_WORDLENGTH_8B,
        UART_STOPBITS_1,
        UART_PARITY_NONE,
        UART_MODE_TX_RX,
        1,
        &uart1_rx_buffer[0],
        UART_BUFFER_CACHE_SIZE
    },
};

static struct device uart1_device={
    "/dev/uart1",
    "usart",
     0,
    NULL,
    (sj_void *)&uart1_data,
};


static DEVICE_MODULE_INIT(uart1_device)

//~~~~~~~~~~~~~~~~~~~~~~USART2~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
static struct rcc_apb_periph uart2_apb[]=
{
    {
        1,RCC_APB1ENR_USART2EN_Pos //slj change
    },
    {
        2,RCC_APB2ENR_IOPAEN_Pos,
    },
    {
        2,RCC_APB2ENR_AFIOEN_Pos,
    },
    {
        0,0,
    }
};

static struct uart_gpio uart2_gpio=
{
    {
        GPIOA_BASE,2,GPIO_MODE_AF_PP,GPIO_SPEED_FREQ_HIGH,GPIO_PULLUP,
    },
    {
        GPIOA_BASE,3,GPIO_MODE_AF_INPUT,GPIO_SPEED_FREQ_HIGH,GPIO_PULLUP,
    },
};

static sj_char uart2_rx_buffer[UART_BUFFER_CACHE_SIZE]={0};

static struct uart_device_data uart2_data={
    &uart2_apb[0],
    &uart2_gpio,
    {
        USART2_IRQn,2,3
    },
    {
        USART2_BASE, //slj change
        115200,
        UART_WORDLENGTH_8B,
        UART_STOPBITS_1,
        UART_PARITY_NONE,
        UART_MODE_TX_RX,
        2,
        &uart2_rx_buffer[0],
        UART_BUFFER_CACHE_SIZE
    },
};

static struct device uart2_device={
    "/dev/uart2",
    "usart",
     0,
    NULL,
    (sj_void *)&uart2_data,
};


static DEVICE_MODULE_INIT(uart2_device)

//~~~~~~~~~~~~~~~~~~~~~~~~USART3~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

static struct rcc_apb_periph uart3_apb[]=
{
    {
        1,RCC_APB1ENR_USART3EN_Pos //slj change
    },
    {
        2,RCC_APB2ENR_IOPBEN_Pos,
    },
    {
        2,RCC_APB2ENR_AFIOEN_Pos,
    },
    {
        0,0,
    }
};

static struct uart_gpio uart3_gpio=
{
    {
        GPIOB_BASE,10,GPIO_MODE_AF_PP,GPIO_SPEED_FREQ_HIGH,GPIO_PULLUP,
    },
    {
        GPIOB_BASE,11,GPIO_MODE_AF_INPUT,GPIO_SPEED_FREQ_HIGH,GPIO_PULLUP,
    },
};

static sj_char uart3_rx_buffer[UART_BUFFER_CACHE_SIZE]={0};
static struct uart_device_data uart3_data={
    &uart3_apb[0],
    &uart3_gpio,
    {
        USART3_IRQn,2,3
    },
    {
        USART3_BASE, //slj change
        115200,
        UART_WORDLENGTH_8B,
        UART_STOPBITS_1,
        UART_PARITY_NONE,
        UART_MODE_TX_RX,
        3,
        &uart3_rx_buffer[0],
        UART_BUFFER_CACHE_SIZE
    },
};

static struct device uart3_device={
    "/dev/uart3",
    "usart",
     0,
    NULL,
    (sj_void *)&uart3_data,
};


static DEVICE_MODULE_INIT(uart3_device)

//~~~~~~~~~~~~~~~~~~~~~~~~UART4~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
static struct rcc_apb_periph uart4_apb[]=
{
    {
        1,RCC_APB1ENR_UART4EN_Pos //slj change
    },
    {
        2,RCC_APB2ENR_IOPCEN_Pos,
    },
    {
        2,RCC_APB2ENR_AFIOEN_Pos,
    },
    {
        0,0,
    }
};

static struct uart_gpio uart4_gpio=
{
    {
        GPIOC_BASE,10,GPIO_MODE_AF_PP,GPIO_SPEED_FREQ_HIGH,GPIO_PULLUP,
    },
    {
        GPIOC_BASE,11,GPIO_MODE_AF_INPUT,GPIO_SPEED_FREQ_HIGH,GPIO_PULLUP,
    },
};



static sj_char uart4_rx_buffer[UART_BUFFER_CACHE_SIZE]={0};
static struct uart_device_data uart4_data={
    &uart4_apb[0],
    &uart4_gpio,
    {
        UART4_IRQn,2,3
    },
    {
        UART4_BASE, //slj change
        115200,
        UART_WORDLENGTH_8B,
        UART_STOPBITS_1,
        UART_PARITY_NONE,
        UART_MODE_TX_RX,
        4,
        &uart4_rx_buffer[0],
        UART_BUFFER_CACHE_SIZE
    },
};

static struct device uart4_device={
    "/dev/uart4",
    "usart",
     0,
    NULL,
    (sj_void *)&uart4_data,
};


static DEVICE_MODULE_INIT(uart4_device)
//~~~~~~~~~~~~~~~~~UART5~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
static struct rcc_apb_periph uart5_apb[]=
{
    {
        1,RCC_APB1ENR_UART5EN_Pos //slj change
    },
    {
        2,RCC_APB2ENR_IOPCEN_Pos,
    },
    {
        2,RCC_APB2ENR_IOPDEN_Pos,
    },
    {
        2,RCC_APB2ENR_AFIOEN_Pos,
    },
    {
        0,0,
    }
};

static struct uart_gpio uart5_gpio=
{
    {
        GPIOC_BASE,12,GPIO_MODE_AF_PP,GPIO_SPEED_FREQ_HIGH,GPIO_PULLUP,
    },
    {
        GPIOD_BASE,2,GPIO_MODE_AF_INPUT,GPIO_SPEED_FREQ_HIGH,GPIO_PULLUP,
    },
};


static sj_char uart5_rx_buffer[UART_BUFFER_CACHE_SIZE]={0};
static struct uart_device_data uart5_data={
    &uart5_apb[0],
    &uart5_gpio,
    {
        UART5_IRQn,2,3
    },
    {
        UART5_BASE, //slj change
        115200,
        UART_WORDLENGTH_8B,
        UART_STOPBITS_1,
        UART_PARITY_NONE,
        UART_MODE_TX_RX,
        5,
        &uart5_rx_buffer[0],
        UART_BUFFER_CACHE_SIZE
    },
};

static struct device uart5_device={
    "/dev/uart5",
    "usart",
     0,
    NULL,
    (sj_void *)&uart5_data,
};


static DEVICE_MODULE_INIT(uart5_device)
